package com.winHandle.load;

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import com.bean.Field;
import com.bean.Table;
import com.common.FrameUtil;
import com.common.MessageShow;
import com.service.impl.FieldServiceImpl;

public class FieldWin extends JFrame implements ActionListener{

	private static final long serialVersionUID = 4144290344297676214L;

	private int weight = 300;
	private int height = 350;
	private boolean canUpdate = true;
	private DefaultTableModel model ;
	@SuppressWarnings("unused")
	private String search_T;
	private String search_F;
	private Table tableBean;
	@SuppressWarnings("unused")
	private List<Field> resuList;
	private Field obj;
	private Long id ;

	private JFrame jframe ;

	private JLabel tableLab ;
	private JLabel tableLab_v ;

	private JLabel seq ;
	private JTextField seq_v ;

	private JLabel name ;
	private JTextField name_v;

	private JLabel detail ;
	private JTextField detail_v ;

	private JButton commBtn ;

	public void excute(){

		int yComp = 40;
		int incr = 40;
		jframe = this;
		if(null != id){
			obj = new FieldServiceImpl().get(id);
		}

		this.setSize( weight, height);
		Toolkit kit = Toolkit.getDefaultToolkit();
		this.setLocation((kit.getScreenSize().width-weight)/2, (kit.getScreenSize().height-height)/2);
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

		this.setLayout(null);

		tableLab = FrameUtil.getJLabel("表单", 30, yComp, 67, 18);
		if(null != id){

			tableLab_v = FrameUtil.getJLabel(obj.getpName() , 100, yComp, 130, 18);
		}else{

			tableLab_v = FrameUtil.getJLabel(tableBean.getName() , 100, yComp, 130, 18);
		}
		yComp += incr;

		seq = FrameUtil.getJLabel("排序", 30, yComp, 67, 18);
		seq_v = FrameUtil.getJTextField(100,yComp,130,18);
		yComp += incr;

		name = FrameUtil.getJLabel("字段", 30, yComp, 67, 18);
		name_v = FrameUtil.getJTextField(100,yComp,130,18);
		yComp += incr;

		detail = FrameUtil.getJLabel("描述", 30, yComp, 67, 18);
		detail_v = FrameUtil.getJTextField(100,yComp,130,18);
		yComp += incr;

		if(null != obj){
			seq_v.setText(obj.getSeq()+"");
			name_v.setText(obj.getName());
			detail_v.setText(obj.getModify());
		}else{
			seq_v.setText("10");
		}

		if(canUpdate){//

			yComp += 20;
			commBtn = FrameUtil.getJButton("    提   交    ", 70,yComp,120,25);

			commBtn.addActionListener(this);
		}

		FrameUtil.addIcon(this);
		this.add(tableLab);
		this.add(tableLab_v);
		this.add(seq);
		this.add(seq_v);
		this.add(name);
		this.add(name_v);
		this.add(detail);
		this.add(detail_v);
		this.add(commBtn);
		this.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		if(e.getSource() == commBtn){

			String seqStr = seq_v.getText();
			String nameStr = name_v.getText();
			String detailStr = detail_v.getText();

			if("".equals(nameStr)){
				MessageShow.warn("表名不能为空！");
			}else{

				if(null == id){

					obj = new Field();
					if("".equals(seqStr)){
						obj.setSeq(10);
					}else{
						try {
							obj.setSeq(Integer.parseInt(seqStr));
						} catch (NumberFormatException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
							MessageShow.error("排序只能填写整数！");
							return ;
						}
					}
					obj.setName(nameStr);
					obj.setModify(detailStr);
					obj.setpId(tableBean.getId());

					if(new FieldServiceImpl().add(obj)){

						MessageShow.information("添加成功！");
						resuList = new FieldServiceImpl().loadModelData(model, tableBean.getName(), search_F);
						jframe.dispose();

					}else{
						MessageShow.error("添加失败！");
					}
				}else{

					if(null == obj){
						MessageShow.error("参数错误！");
					}else{
						boolean update = false;
						if(obj.getSeq() != Integer.parseInt(seqStr)){
							try {
								obj.setSeq(Integer.parseInt(seqStr));
							} catch (NumberFormatException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
								MessageShow.error("排序只能填写整数！");
								return ;
							}
							update = true;
						}
						if(!obj.getName().trim().equals(nameStr.trim())){
							obj.setName(nameStr);
							update = true;
						}
						if(!obj.getModify().trim().equals(detailStr.trim())){
							obj.setModify(detailStr);
							update = true;
						}
						if(update){
							if(new FieldServiceImpl().update(obj)){

								MessageShow.information("修改成功！");

								resuList = new FieldServiceImpl().loadModelData(model, obj.getpName(), search_F);
								jframe.dispose();

							}else{
								MessageShow.error("修改失败！");
							}
						}else{
							MessageShow.warn("内容未做修改！");
							jframe.dispose();
						}

					}
				}
			}

		}

	}

	/**
	 *
	 * @param title 弹框标题
	 * @param model
	 * @param id	修改的记录id，增加记录是此字段可为空
	 * @param searchTXT 搜索框内容，增加记录时此字段可为空
	 * @param canUpdate 弹框是否要有修改功能
	 */
	public FieldWin(String title, DefaultTableModel model, Long id, String search_T, String search_F, boolean canUpdate, List<Field> resuList){
		super(title);
		this.id = id;
		this.model = model;
		this.search_T = search_T;
		this.search_F = search_F;
		this.canUpdate = canUpdate;
		this.resuList = resuList;
		excute();
	}

	public FieldWin(String title, DefaultTableModel model, Table tableBean, String search_F){
		super(title);
		this.model = model;
		this.tableBean = tableBean;
		this.search_F = search_F;
		excute();
	}

	public FieldWin() {
		super();
		excute();
	}

}
